using System;
using System.Web;
using System.Web.Caching;
using System.Configuration;
using System.Xml.Serialization;

namespace URLRewriter.Config
{
    //Specifies the configuration settings in the Web.config for the RewriterRule.   This class defines the structure of the Rewriter configuration file in the Web.config file.  Currently, it allows only for a set of rewrite rules; however, this approach allows for customization.  For example, you could provide a ruleset that doesn't use regular expression matching; or a set of  constant names and values, which could then be referenced in rewrite rules.  

    // The structure in the Web.config file is as follows:
    //<configuration>
    //  <configSections>
    //    <section name="RewriterConfig"  type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
    //  </configSections>
    //  <RewriterConfig>
    //    <Rules>
    //      <RewriterRule>
    //        <LookFor>pattern</LookFor>
    //        <SendTo>replace with</SendTo>
    //      </RewriterRule>
    //      <RewriterRule>
    //        <LookFor>pattern</LookFor>
    //        <SendTo>replace with</SendTo>
    //      </RewriterRule>
    //      ...
    //      <RewriterRule>
    //        <LookFor>pattern</LookFor>
    //        <SendTo>replace with</SendTo>
    //      </RewriterRule>
    //    </Rules>
    //  </RewriterConfig>
    //  <system.web> ... </system.web>
    //</configuration>
	[Serializable()]
	[XmlRoot("RewriterConfig")]
	public class RewriterConfiguration
	{
		// private member variables
		private RewriterRuleCollection rules;			// an instance of the RewriterRuleCollection class...

		/// <summary>
		/// GetConfig() returns an instance of the <b>RewriterConfiguration</b> class with the values populated from
		/// the Web.config file.  It uses XML deserialization to convert the XML structure in Web.config into
		/// a <b>RewriterConfiguration</b> instance.
		/// </summary>
		/// <returns>A <see cref="RewriterConfiguration"/> instance.</returns>
		public static RewriterConfiguration GetConfig()
		{
			if (HttpContext.Current.Cache["RewriterConfig"] == null)
				HttpContext.Current.Cache.Insert("RewriterConfig", ConfigurationSettings.GetConfig("RewriterConfig"));

			return (RewriterConfiguration) HttpContext.Current.Cache["RewriterConfig"];
		}

		#region Public Properties
		/// <summary>
		/// A <see cref="RewriterRuleCollection"/> instance that provides access to a set of <see cref="RewriterRule"/>s.
		/// </summary>
		public RewriterRuleCollection Rules
		{
			get
			{
				return rules;
			}
			set
			{
				rules = value;
			}
		}
		#endregion
	}
}
